home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / impr_base.idb / usr / lib / print / data / PSImageProlog.z / PSImageProlog
Encoding:
Text File  |  1997-07-30  |  6.8 KB  |  233 lines

  1. %!PS-Adobe-3.0 Resource-ProcSet
  2. %%Title: PSImageProlog
  3. %%Copyright: 1993 Silicon Graphics, Inc. All rights reserved.
  4. %  Procedure: processImage
  5. %
  6. %  Description:
  7. %    This PostScript code prolog defines a procedure for displaying
  8. %    a bitmap image. The procedure provides for zooming, rotating
  9. %    and mirroring the image. In addition, the procedure accepts
  10. %    both ASCII hex image data and binary image data.
  11. %
  12. %  Syntax:
  13. %    IWidth IHeight IBits Binary NChans Angle BestFit FlipY RasDir
  14. %    Zoom IRes NUpx NUpy NPos processImage
  15. %
  16. %  Parameters:
  17. %    IWidth, IHeight    - Image width and height in pixels
  18. %    IBits        - Bits per color component (1, 2, 4, or 8)
  19. %    Binary        - Data type: 0 = ASCII hex, 1 = binary
  20. %    NChans        - Number of data channels (1, 3 or 4)
  21. %    Angle        - Image rotation angle in degress
  22. %    BestFit        - Image orientation: 0 = use angle, 1 = best fit
  23. %    FlipY        - Mirror image about Y axis: 0 = no, 1 = flip
  24. %    RasDir        - Raster direction: 0 = top to bottom, 1 = bot. to top
  25. %    Zoom        - Zoom factor: 0.0 = no zoom, 1.0 = zoom to fit
  26. %              imageable area, 0.5 = zoom to 50% of imageable area
  27. %    IRes        - Render image assuming specified resolution
  28. %    NUpx, NUpy    - Place nupx by nupy images per page
  29. %    NPos        - Position in nup sequence (0, 1, ..., nup-1)
  30. %%BeginResource: procset processImage 1.2 0
  31.  
  32. /processImage {
  33.  
  34.     % Get procedure parameters
  35.  
  36.     /NPos    exch def        % N up position
  37.     /NUpY    exch def        % N up in Y direction
  38.     /NUpX    exch def        % N up in X direction
  39.     /IRes    exch def        % Required image resolution
  40.     /Zoom    exch def        % Zoom factor
  41.     /RasDir  exch def        % Raster direction
  42.     /FlipY   exch def        % Mirror about Y
  43.     /BestFit exch def        % Page orientation flag
  44.     /Angle   exch def        % Rotation angle (degrees)
  45.     /NChans  exch def        % Number of image data channels
  46.     /Binary  exch def        % Binary data flag
  47.     /IBits   exch def        % Bits per component
  48.     /IHeight exch def        % Image height
  49.     /IWidth  exch def        % Image width
  50.  
  51.     % Get the frame buffer dimensions
  52.  
  53.     newpath clippath pathbbox
  54.     /URy exch def
  55.     /URx exch def
  56.     /LLy exch def
  57.     /LLx exch def
  58.     /FBWidth  URx LLx sub def
  59.     /FBHeight URy LLy sub def
  60.  
  61.     % If we are doing N up calculate new frame buffer dims and
  62.     % image origin coords
  63.  
  64.     NUpX 1 gt NUpY 1 gt or {
  65.     /MarX 10 def    % Inter-image margins
  66.     /MarY 10 def
  67.     /FBWidth FBWidth NUpX 1 add MarX mul sub NUpX div def
  68.     /FBHeight FBHeight NUpY 1 add MarY mul sub NUpY div def
  69.     /LLx MarX FBWidth add NPos NUpX mod mul MarX add LLx add def
  70.     /LLy NUpY NPos NUpX NUpY mul mod NUpX idiv sub 1 sub
  71.                 MarY FBHeight add mul MarY add LLy add def
  72.     } if
  73.  
  74.     % Compute aspect ratios
  75.  
  76.     /FBAspect FBWidth FBHeight div def
  77.     /IAspect IWidth IHeight div def
  78.  
  79.     % If best fit desired, determine best fit rotation angle.
  80.     % The best fit occurs when the image and frame buffer aspect
  81.     % ratios are both on the same side of 1.0
  82.  
  83.     BestFit 1 eq {
  84.     FBAspect 1.0 lt IAspect 1.0 lt xor {
  85.         /Angle 90 def
  86.     } {
  87.         /Angle 0 def
  88.     } ifelse
  89.     }
  90.  
  91.     % Otherwise normalize the rotation angle to 0 <= Angle < 360
  92.     % so that we can perform bounding box calc and we only have
  93.     % to special case a few angles
  94.  
  95.     {
  96.     /Angle Angle 360 mod dup 0 lt {
  97.         360 add
  98.     } if def
  99.     } ifelse
  100.  
  101.     % Compute the bounding box dimensions of the rotated image
  102.     % and the bounding box aspect ratio.
  103.  
  104.     Angle 0 eq Angle 180 eq or {
  105.     /IBWidth IWidth def
  106.     /IBHeight IHeight def
  107.     /IBAspect IAspect def
  108.     } {
  109.     Angle 90 eq Angle 270 eq or {
  110.         /IBWidth IHeight def
  111.         /IBHeight IWidth def
  112.         /IBAspect IHeight IWidth div def
  113.     } {
  114.         /AbsSin Angle sin abs def
  115.         /AbsCos Angle cos abs def
  116.         /IBWidth IWidth AbsCos mul IHeight AbsSin mul add def
  117.         /IBHeight IWidth AbsSin mul IHeight AbsCos mul add def
  118.             /IBAspect IBWidth IBHeight div def
  119.     } ifelse
  120.     } ifelse
  121.  
  122.     % Calculate the scaled image bounding box dimensions. This is
  123.     % also where we apply the zoom factor or ppi scaling.
  124.  
  125.     IRes 0 eq {
  126.     Zoom 0.0 gt {            % Zoom as fraction imageable area
  127.             FBAspect IBAspect lt {
  128.             /XBSize FBWidth Zoom mul def
  129.             /YBSize FBWidth IBAspect div Zoom mul def
  130.             } {
  131.             /XBSize FBHeight IBAspect mul Zoom mul def
  132.             /YBSize FBHeight Zoom mul def
  133.             } ifelse
  134.         } {                % No zoom, 1 dot in == 1 dot out 
  135.         /XBSize IBWidth 1 1 dtransform pop abs div def
  136.         /YBSize IBHeight 1 1 dtransform exch pop abs div def
  137.     } ifelse
  138.     } {                    % Required image resolution (ppi)
  139.     /XBSize IBWidth IRes 72 div div def
  140.     /YBSize IBHeight IRes 72 div div def
  141.     } ifelse
  142.  
  143.     % Calculate the scaled image dimensions
  144.  
  145.     Angle 0 eq Angle 180 eq or {
  146.     /XISize XBSize def
  147.     /YISize YBSize def
  148.     } {
  149.     Angle 90 eq Angle 270 eq or {
  150.         /XISize YBSize def
  151.         /YISize XBSize def
  152.     } {
  153.         /XISize XBSize AbsCos 1.0 IAspect div AbsSin mul add div def
  154.         /YISize XBSize IAspect AbsCos mul AbsSin add div def
  155.     } ifelse
  156.     } ifelse
  157.  
  158.     % Translate origin of imageable area
  159.  
  160.     LLx LLy translate
  161.  
  162.     % Center the image
  163.  
  164.     FBWidth XBSize sub 2 div
  165.     FBHeight YBSize sub 2 div
  166.     translate
  167.  
  168.     % Rotate the image about its center, then mirror it
  169.     % about y if flip is specified
  170.  
  171.     XBSize 2 div YBSize 2 div translate
  172.     FlipY 1 eq {
  173.         1.0 neg 1.0 scale
  174.     } if
  175.     Angle rotate
  176.     XISize 2 div neg YISize 2 div neg translate
  177.  
  178.     % Scale the image to the desired size
  179.  
  180.     XISize YISize scale
  181.  
  182.     % Define a data reading procedure appropriate to
  183.     % the image data type
  184.  
  185.     Binary 0 eq {
  186.     /getByte { readhexstring } bind def
  187.     } {
  188.     /getByte { readstring } bind def
  189.     } ifelse
  190.  
  191.     % Execute the sampling operator appropriate to the
  192.     % colorspace of the data. That is, 'image' if number of
  193.     % channels is 1 (e.g. W), 'colorimage' if it is 3 (e.g. RGB)
  194.     % or 4 (e.g. CMYK)
  195.     %
  196.     % Even though the Redbook indicates the string can be any size
  197.     % experience indicates that string should be the number of
  198.     % bytes per row.
  199.  
  200.     /IRowStr NChans IWidth mul IBits
  201.         mul 7 add 8 idiv string def     % Allocate row bytes of string
  202.     IWidth IHeight IBits        % Image width, height, bpp
  203.     RasDir 0 eq {            % Sample based on raster direction
  204.         [IWidth 0 0 IHeight neg 0 IHeight]    % Top to bottom
  205.     } {
  206.         [IWidth 0 0 IHeight 0 0]        % Bottom to top
  207.     } ifelse
  208.     { currentfile IRowStr getByte pop } bind    % Sampling proc
  209.     NChans 1 eq {                % Use image if black and white
  210.         image
  211.     } {
  212.     systemdict /colorimage known {        % Only if have colorimage
  213.         false NChans            % Interleaved data
  214.         colorimage
  215.     } {                    % If no colorimage print error
  216.         initgraphics
  217.         /Courier findfont 12 scalefont setfont
  218.         LLx 5 add FBHeight 2 div 2 copy moveto
  219.         (PSImageProlog Error: 'colorimage' operator not available,)
  220.         show
  221.         15 sub moveto
  222.         (                     re-render image in black and white \(w\).)
  223.         show
  224.         showpage
  225.         quit
  226.     } ifelse
  227.     } ifelse
  228. } bind def
  229.  
  230. %%EndResource
  231.